package com.dragee.modules.work.controller;

import com.dragee.common.PageUtils;
import com.dragee.common.R;
import com.dragee.modules.sys.entity.LoginUser;
import com.dragee.modules.sys.po.RolePo;
import com.dragee.modules.work.entity.TeamEntity;
import com.dragee.modules.work.entity.TeamLeaderEntity;
import com.dragee.modules.work.service.TeamLeaderService;
import com.dragee.modules.work.service.TeamService;
import com.dragee.utils.LoginUserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 领队
 *
 * @author tc
 * @email tc@mail.com
 * @date 2023-04-19 09:42:02
 */
@RestController
@RequestMapping("work/teamleader")
@Api(tags = "work - 领队")
public class TeamLeaderController {
    @Autowired
    private TeamLeaderService teamLeaderService;
    @Autowired
    private TeamService teamService;

    /**
     * 列表
     */
    @GetMapping("list")
    @ApiOperation("领队列表")
    public R list(@RequestParam Map<String, Object> params){
        LoginUser loginUser = LoginUserUtil.getLoginUser();
        RolePo role = loginUser.getRole();
        if (role.getRoleKey().equals("duiwu")) {
            // 队伍登录
            TeamEntity team = teamService.getTeamByUserId(loginUser.getUserId().toString());
            params.put("teamKey", team.getId());
        }
        PageUtils page = teamLeaderService.queryPage(params);
        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @GetMapping("{id}")
    @ApiOperation("领队信息")
    public R info(@PathVariable("id") Long id){
		TeamLeaderEntity teamLeader = teamLeaderService.getById(id);
        return R.ok().put("data", teamLeader);
    }

    /**
     * 保存
     */
    @PostMapping("saveOrUpdate")
    @ApiOperation("保存或修改领队")
    public R saveOrUpdate(@RequestBody TeamLeaderEntity teamLeader){
        TeamEntity team = teamService.getTeamByUserId(LoginUserUtil.getLoginUserId().toString());
        teamLeader.setTeamKey(team.getId().toString());
        teamLeader.setTeamId(team.getCode());
        teamLeaderService.saveOrUpdate(teamLeader);
        return R.ok();
    }


    /**
     * 删除
     */
    @GetMapping("delete-by-id")
    @ApiOperation("领队删除")
    public R delete( String id){
		teamLeaderService.removeById(Long.parseLong(id));
        return R.ok();
    }

    @GetMapping("delete-by-ids")
    @ApiOperation("领队批量删除")
    public R deletes(String ids){
        String[] idArr = ids.split(",");
        Arrays.stream(idArr).forEach(id -> teamLeaderService.removeById(Long.parseLong(id)));
        return R.ok();
    }

}
